//
//  loginViewController.swift
//  NeusoftSchool
//
//  Created by 李狗蛋 on 2017/3/13.
//  Copyright © 2017年 李狗蛋. All rights reserved.
//

import UIKit

class loginViewController: UIViewController {

    @IBOutlet weak var userName: UITextField!
    
    @IBOutlet weak var passWord: UITextField!
    
    @IBOutlet weak var checkCodeImage: UIImageView!
    
    @IBOutlet weak var checkCode: UITextField!
    
    @IBAction func login(_ sender: UIButton) {
        
        if userName.text == "" {
            //print("用户名没有输入")
            MBProgressHUD.showDelayHUDToView(self.view, message: "用户名没有输入")
        }else{
            if passWord.text == "" {
                //print("密码没有输入")
                MBProgressHUD.showDelayHUDToView(self.view, message: "密码没有输入")
            }else{
                //验证用户名密码
                let parameters: NSDictionary = ["username": userName.text!,"userpass": passWord.text!]
                RONetworkMngTool.sharedNetwodkMngTool().RONetwork_Login(parameters, view: self.view, block: { (flag) in
                    if flag == "1" {
                        let _ = (ACSimpleKeychain.defaultKeychain() as AnyObject).storeUsername(self.userName.text, password: self.passWord.text, identifier: "user1", forService: "userpassword")
                        print("登陆成功，跳转到主页")
                        UIApplication.shared.keyWindow!.rootViewController = ROMainTabBarViewController()
                    }else{
                        //清空用户名密码
                        print("登陆失败")
                        let _ = (ACSimpleKeychain.defaultKeychain() as AnyObject).deleteAllCredentials(forService: "userpassword")
                        let _ = (ACSimpleKeychain.defaultKeychain() as AnyObject).storeUsername("", password: "", identifier: "user1", forService: "userpassword")
                    }
                })
            }
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        featchUserNamePwd()
        // Do any additional setup after loading the view.
    }
    
    func featchUserNamePwd() {
        let userPwdDict = (ACSimpleKeychain.defaultKeychain() as AnyObject).credentials(forIdentifier: "user1", service: "userpassword") as NSDictionary
        if userPwdDict.count != 0 {
            userName.text = userPwdDict.object(forKey: "username") as? String
            passWord.text = userPwdDict["password"] as? String
        }
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.userName.resignFirstResponder()
        self.passWord.resignFirstResponder()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
